package dsk.demo.product.service.impl;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import dsk.demo.common.utils.PageUtils;
import dsk.demo.product.dao.SupplierProductRefDao;
import dsk.demo.product.dto.SupplierProductQueryDto;
import dsk.demo.product.dto.SupplierProductSaveDto;
import dsk.demo.product.entity.ProductEntity;
import dsk.demo.product.entity.SupplierProductRefEntity;
import dsk.demo.product.service.ProductService;
import dsk.demo.product.service.SupplierProductRefService;
import dsk.demo.product.vo.SupplierProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SupplierProductRefServiceImpl extends ServiceImpl<SupplierProductRefDao, SupplierProductRefEntity> implements SupplierProductRefService {

    @Autowired
    ProductService productService;

    @Override
    public PageUtils querySupplierProductPage(SupplierProductQueryDto queryDto) {
        //分页对象
        Page<SupplierProductVo> page = new Page<>(queryDto.getPage(), queryDto.getLimit());
        IPage<SupplierProductVo> result = this.baseMapper.querySupplierProductPage(page, queryDto);

        return new PageUtils(result);
    }

    @Override
    public List<SupplierProductRefEntity> excel2ProductEntityList(MultipartFile file) {
        try{
            InputStream inputStream = file.getInputStream();
            ExcelReader reader = ExcelUtil.getReader(inputStream);
            Map<String, String> map = new HashMap<>();
            map.put("本厂编号","productNo");
            map.put("工厂交货价","price");
            map.put("工厂产品编号","supplierProductNo");
            map.put("备注","remark");
            reader.setHeaderAlias(map);
            List<SupplierProductSaveDto> all = reader.readAll(SupplierProductSaveDto.class);
            return all.stream().map(item -> {
                SupplierProductRefEntity productRefEntity = new SupplierProductRefEntity();
                Long productId = productService.getIdByProductNo(item.getProductNo());
                productRefEntity.setProductId(productId);
                productRefEntity.setSupplierProductNo(item.getSupplierProductNo());
                productRefEntity.setPrice(item.getPrice());
                productRefEntity.setRemark(item.getRemark());
                return productRefEntity;
            }).filter(item -> item.getProductId() != null && item.getProductId() > 0)
              .collect(Collectors.toList());
        }catch (Exception e){
            return null;
        }
    }

    @Override
    public void insertBatch(List<SupplierProductRefEntity> productEntities) {
        this.baseMapper.insertBatch(productEntities);
    }
}
